package sample;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.sun.javafx.binding.StringFormatter;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

public class Controller implements Initializable {
    public TextField parte;
    public TextField sourceTime;
    public TextField year;
    public TextField mouth;
    public TextField day;
    public TextField outTime;

    private Date sourceDate;
    private Date outDate;


    public void cover(ActionEvent actionEvent) {
        sourceDate = DateUtil.parse(sourceTime.getText());
        outDate = DateUtil.offsetDay(sourceDate, Integer.parseInt(day.getText()));
        outDate = DateUtil.offsetMonth(outDate, Integer.parseInt(mouth.getText()));
        List<String> gf = new ArrayList<>();
        for (char c : sourceTime.getText().toCharArray()) {
            if (!NumberUtil.isNumber("" + c)) {
                gf.add(String.valueOf(c));
                System.out.print(gf);
            }
        }
        String pt = StringFormatter.format(gf.size() == 2 ? "yyyy%sMM%sdd" : "yyyy%sMM%sdd%s", gf.toArray()).getValue();
        String format = DateUtil.format(outDate, pt);
        outTime.setText(format);
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        // 封装文本内容
        Transferable trans = new StringSelection(format);
        // 把文本内容设置到系统剪贴板
        clipboard.setContents(trans, null);
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        sourceTime.textProperty().addListener((observableValue, s, t1) -> cover(null));
    }
}
